/**
 * 
 */
package co.fitstart.entity.content;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.core.utils.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 知识库
 * 
 * @author Leo
 * @author lute
 */
@Entity
@Table(name="content_article")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Article {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 知识库名称 */
    private String name;
    
    /** 知识库封面 */
    private String cover;
    
    /** 知识库简介 */
    private String summary;
    
    /** 知识库链接 */
    private String link;
    
    private Date lastUpdateTime;
    
    @JSONField(format = "yyyy-MM-dd")
    private Date createTime;
    
    @Column(name = "is_disabled")
    @JSONField(serialize = false)
    private boolean disabled;
    
    @JsonIgnore
    @OneToMany(mappedBy = "article", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JSONField(serialize = false)
    private List<ArticleModule> modules;
    
    /**
     * The default construction
     */
    public Article() {
    }
    
    /**
     * 
     * @return
     */
    public Article initialize() {
        this.createTime = new Date();
        this.lastUpdateTime = this.createTime;
        return this;
    }
    
    /**
     * edit article cover
     * 
     * @param cover
     * @return
     */
    public Article editCover(String cover) {
        this.cover = cover;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * Get all module ids
     * 
     * @return
     */
    @JSONField(serialize = false)
    @SuppressWarnings("unchecked")
    public List<Long> getModuleIds() {
        return CollectionUtils.extractPropertyList(modules, "module.id");
    }
    
    /**
     * 
     * @return
     */
    public Article disable() {
        this.disabled = true;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * Update article's last update time
     * 
     * @param lastUpdateTime
     * @return
     */
    public Article lastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
        return this;
    }
    
    /**
     * Determine whether the news contains the one of the article module
     * 
     * @param moduleId
     * @return
     */
    public boolean containsModule(Long moduleId) {
        List<Long> paymentMethods = this.getModuleIds();
        if(paymentMethods != null){
            return paymentMethods.contains(moduleId);
        }
        return false;
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the cover
     */
    public String getCover() {
        return cover;
    }

    /**
     * @return the summary
     */
    public String getSummary() {
        return summary;
    }
    
    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }
    
    /**
     * @return the lastUpdateTime
     */
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @return the modules
     */
    public List<ArticleModule> getModules() {
        return modules;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param cover the cover to set
     */
    public void setCover(String cover) {
        this.cover = cover;
    }

    /**
     * @param summary the summary to set
     */
    public void setSummary(String summary) {
        this.summary = summary;
    }
    
    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }
    
    /**
     * @param lastUpdateTime the lastUpdateTime to set
     */
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @param modules the modules to set
     */
    public void setModules(List<ArticleModule> modules) {
        this.modules = modules;
    }

}
